<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\api\model\Homework;
use think\Db;

/**
 * 听力接口
 */
class Listening extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];
    protected $lp = 'listening_products';//听力 商品教材表
    protected $lc = 'listening_catalogues';//听力 章节表
    protected $lr = 'listening_resources';//听力 资源表
    protected $asd = 'association_data';//关联数据设置表
    /**
     * 听力列表
     *
     */
    public function index()
    {
        $grade = input('grade/d'); //年级
        $page = input('page/d')&&is_numeric(input('page/d')) ? input('page/d') : 1;
        $pageSize = input('pageSize/d')&&is_numeric(input('pageSize/d')) ? input('pageSize/d') : 10;
        $assarr = [];
        $assarr = Db::name($this->asd)->where(['type'=>'listening','status'=>2])->column('rid');
        $where = [];
        if ($grade) {
            $where['grade'] = ['=',$grade];
        }
        //增加关键词搜索
        $keyword = input('keyword');
        if ($keyword) {
            $where['productName'] = ['like','%'.$keyword.'%'];
        }
        //var_dump($where);die();
        //关键词搜索条件结束
        $lpdata = Db::name($this->lp)->where($where)->whereNotIn('productId',$assarr)->order('id asc')->limit(($page - 1)*$pageSize,$pageSize)->select();
        $parr = Db::name('teacher_collect')->where(['uid'=>$this->auth->id,'status'=>1])->column('pid');
        foreach ($lpdata as &$vs) {
            $vs['collect'] = 0;
            if (in_array($vs['productId'],$parr)) {
                $vs['collect'] = 1;
            }
            $vs['cover_full'] = cdnurl($vs['cover'],true);
        }
        unset($vs);
        $this->success('请求成功',$lpdata);
    }
    /**
     * 听力详情
     *
     */
    public function detail()
    {
        $id = input('id/d'); //听力商品id
        if (!$id) {
            $this->error(__('Invalid parameters'));
        }
        $where = [];
        if ($id) {
          $where['productId'] = $id;  
        }
        $parr = Db::name('teacher_collect')->where(['uid'=>$this->auth->id,'status'=>1])->column('pid');
        $lpdata = Db::name($this->lp)->where($where)->find();
        $lpdata['collect'] = 0;
        if (in_array($lpdata['productId'],$parr)) {
            $lpdata['collect'] = 1;
        }
        $lpdata['cover_full'] = cdnurl($lpdata['cover'],true);
        //$lpdata['lc'] = Db::name($this->lc)->where($where)->order('displayOrder desc')->select();
        //$lpdata['lc_num'] = count($lpdata['lc']);
        $this->success('请求成功',$lpdata);
    }

    /**
     * 听力章节列表
     *
     */
    public function chapterlist()
    {
        $id = input('id/d'); //听力商品id
        $order = input('order/s')?input('order/s'):'asc'; //听力商品章节排序 升序：asc 降序：desc
        $page = input('page/d')&&is_numeric(input('page/d')) ? input('page/d') : 1;
        $pageSize = input('pageSize/d')&&is_numeric(input('pageSize/d')) ? input('pageSize/d') : 10;
        if (!$id) {
            $this->error(__('Invalid parameters'));
        }
        $where = [];
        if ($id) {
          $where['productId'] = $id;  
        }
        if ($order) {
            $order = 'displayOrder '.$order;
        }
        $lcdata = Db::name($this->lc)->where($where)->order($order)->limit(($page - 1)*$pageSize,$pageSize)->select();
        
        $this->success('请求成功',$lcdata);
    }

    /**
     * 听力章节详情
     *
     */
    public function chapterdetail()
    {
        $id = input('id/d'); //听力商品章节id
        $pid = input('pid/d');//听力商品教材id
        if (!$id) {
            $this->error(__('Invalid parameters'));
        }
        $where = [];
        if ($id) {
          $where['id'] = $id;  
        }
        $lcdata = Db::name($this->lc)->where($where)->find();
        $lr = Db::name($this->lr)->where(['cId'=>$id,'productId'=>$pid])->order('id asc')->select();
        $homework = new Homework();
        foreach ($lr as &$vs) {
            $vs['gunzipResPath_full'] = cdnurl($vs['gunzipResPath'],true);
            $resource = json_decode(file_get_contents($vs['gunzipResPath_full'].'/resource/resource.json'),true);
            $vs['tnum'] = count($resource['items']);
            $vs['totalScore'] = $resource['totalScore'];
            $vs['minute'] = $vs['tnum']*3;
            $vs['scoringrate'] = '0%';
            $vs['usenum'] = $homework->where(['uid'=>$this->auth->id,'ridjson'=>['like','%'.$vs['versionId'].'%'],'status'=>1])->count();
        }
        unset($vs);
        $lcdata['lr'] = $lr;
        $this->success('请求成功',$lcdata);
    }

    /**
     * 听力模拟测试题
     *
     */
    public function questions()
    {
        $id = input('id/s'); //听力商品测试题id
        if (!$id) {
            $this->error(__('Invalid parameters'));
        }
        $where = [];
        if ($id) {
          $where['id'] = $id;  
        }
        $lr = Db::name($this->lr)->where('versionId',$id)->find();
        $lr['gunzipResPath_full'] = fileurl($lr['gunzipResPath'],true);
        $resource = json_decode(file_get_contents($lr['gunzipResPath_full'].'/resource/resource.json'),true);
        $resource_h5 = json_decode(file_get_contents($lr['gunzipResPath_full'].'/resource/resource_h5.json'),true);
        
        
        $data = [
            'resmain' => fileurl('/file/listening/res/',true),
            'resource'=>$resource,
            'resource_h5'=>$resource_h5,
        ];
        $this->success('请求成功',$data);
    }
}
